package com.yyfax.ph.personal.authority.verifycode;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.secure.RSA;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.GlobalVariableConfig;
import com.yyfax.ph.personal.authority.RSAPresenter;
import com.yyfax.ph.personal.authority.forgot.ForgotPwdActivity;
import com.yyfax.ph.personal.authority.phonechange.ChangeOldPhoneActivity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * P层：获取、校验验证码
 *
 * @author ying_he
 * @date 2017/3/7 17:43
 */
public class VerifyCodePresenter extends RSAPresenter<VerifyCodeModel, VerifyCodePresenter.View> {
	private static final String TAG = VerifyCodePresenter.class.getSimpleName();
	private View mView;
	private String mCurrentPage;

	public static class Generator implements PresenterFactory<VerifyCodePresenter> {
		@Override
		public VerifyCodePresenter create() {
			return new VerifyCodePresenter();
		}
	}

	@Override
	public void attach(View view) {
		mCurrentPage = view.getClass().getName();
		mView = view;
		mModel = new VerifyCodeModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		VerifyCodeModel.VerifyCodeParams getVerifyParams(String verifyCodeType);

		void obtainVerifyCodeSuccess(String verifyCode);

		void obtainVerifyCodeFailure(String tips);

		void checkVerifyCodeSuccess(String code);

		void checkVerifyCodeFailure(String tips);
	}

	@Override
	public Object getRSAParams(Object params, int functionalCode) {
		VerifyCodeModel.VerifyCodeParams verifyCode = (VerifyCodeModel.VerifyCodeParams) params;
		String keyRSA = GlobalVariableConfig.getKeyRSA();
		verifyCode.phone = RSA.encryptByPublicKey(verifyCode.phone, keyRSA);
		return verifyCode;
	}

	/**
	 * 获取验证码
	 *
	 * @param verifyCodeType 验证码类型，详见 {@link VerifyCodeModel#VERIFY_CODE_TYPE_USER_REGISTER} 常量声明
	 */
	public void obtainVerifyCode(String verifyCodeType) {
		execute(mView.getVerifyParams(verifyCodeType), VerifyCodeModel.MSG_OBTAIN_VERIFY_CODE);
	}

	/**
	 * 校验验证码
	 *
	 * @param verifyCodeType 验证码类型，详见 {@link VerifyCodeModel#VERIFY_CODE_TYPE_USER_REGISTER} 常量声明
	 */
	public void checkVerifyCode(String verifyCodeType) {
		execute(mView.getVerifyParams(verifyCodeType), VerifyCodeModel.MSG_CHECK_VERIFY_CODE);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void obtainVerifyCodeResult(VerifyCodeModel.ObtainVerifyCodeResult obtainVerifyCodeResult) {
		if (isResponseSuccess(obtainVerifyCodeResult)) {
			String verifyCode = "";
			if (obtainVerifyCodeResult.getContent() != null) {
				verifyCode = obtainVerifyCodeResult.getContent().code;
			}
			mView.obtainVerifyCodeSuccess(verifyCode);
		} else {
			mView.obtainVerifyCodeFailure(obtainVerifyCodeResult.getMsg());
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void checkVerifyCodeResult(VerifyCodeModel.CheckVerifyCodeResult checkVerifyCodeResult) {
		if(TextUtils.equals(mCurrentPage, ChangeOldPhoneActivity.class.getName())) {
			StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_E024, checkVerifyCodeResult.getStatus());
		}else if(TextUtils.equals(mCurrentPage, ForgotPwdActivity.class.getName())){
			StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_E041, checkVerifyCodeResult.getStatus());
		}
		if (isResponseSuccess(checkVerifyCodeResult)) {
			mView.checkVerifyCodeSuccess(checkVerifyCodeResult.getContent().cid);
		} else if (isResponseWarning(checkVerifyCodeResult)) {
			if (checkVerifyCodeResult.getContent() != null && "10008".equals(checkVerifyCodeResult.getContent().code)) {//用于清空验证码
				mView.checkVerifyCodeFailure("验证码错误");
			} else {
				mView.checkVerifyCodeFailure(TextUtils.isEmpty(checkVerifyCodeResult.getMsg()) ? "校验失败!" : checkVerifyCodeResult.getMsg());
			}
		} else {
			mView.checkVerifyCodeFailure(TextUtils.isEmpty(checkVerifyCodeResult.getMsg()) ? "校验失败!" : checkVerifyCodeResult.getMsg());
		}
	}

}
